<template>
	<tm-app ref="app">
		<tm-navbar title="订单详情" color="orange" linear="right" beforeBack="false" hideHome="false" @click="backPage"></tm-navbar>
		<tm-sheet :margin="[32,0]" :padding="[0]" >
			<tm-descriptions :label-width="120" :width="686" :column="2" transprent :round="10" text>
				<tm-descriptions-item :font-size="27" v-for="(item,index) in list" :key="index" :label="item.label"
					:value="item.value"></tm-descriptions-item>
			</tm-descriptions>
			<tm-descriptions :label-width="150" :width="686" :column="1" transprent :round="10" text>
				<tm-descriptions-item color="orange" :font-size="35" v-for="(item,index) in list2" :key="index"
					:label="item.label" :value="item.value"></tm-descriptions-item>
			</tm-descriptions>
			<tm-descriptions :label-width="120" :width="686" :column="2" transprent :round="2" text>
				<tm-descriptions-item :font-size="27" v-for="(item,index) in list3" :key="index" :label="item.label"
					:value="item.value"></tm-descriptions-item>
			</tm-descriptions>
			
			<tm-sheet v-if="sta" >
			    
			</tm-sheet>
		</tm-sheet>
	</tm-app>
</template>


<script lang="ts" setup>
	import { ref } from "vue"
	import { onLoad } from "@dcloudio/uni-app"
	import { orderDetailById, wxPayOrderMM } from '@/api/order'
	import { isEmpty } from "@/tmui/tool/function/util";


	const userid = ref("");
	const orderid = ref("");
	const flag = ref(false);
	const msg = ref("");
	const list = ref([]);
	const list2 = ref([]);
	const list3 = ref([]);
	const sta = ref(false);
	onLoad((e) => {
		console.log(JSON.stringify(e));
		// 进行扫码计时开始和扫码计时结束的请求
		userid.value = uni.getStorageSync("userid");
		if (isEmpty(userid.value)) {
			uni.redirectTo({
				url: '/pages/auth/auth'
			})
		} else {
			orderid.value = e.scene;
			loadOrderDetail();
		}
	})

	const loadOrderDetail = async () => {
		let param = { "wxid": userid.value, "orderid": orderid.value };
		const result = await orderDetailById(param); // 进行获取订单的基本信息；进行面对面付款
		sta.value = result.status;
		// console.log(result);
		// console.log(result.status==='5');
		// console.log(sta.value==='5');
		if(result.status==='5'){
			sta.value = true;
		}
		
		list.value.push({ label: '家长姓名', value: result.lxr });
		list.value.push({ label: '联系方式', value: result.phone });
		list.value.push({ label: '帮手姓名', value: result.name });
		list.value.push({ label: '联系方式', value: result.mobile });
		list2.value.push({ label: '地址', value: result.address });
		list2.value.push({ label: '订单状态', value: statusText(result.status) });
		list3.value.push({ label: '科目', value: result.km });
		list3.value.push({ label: '--', value: '--' });
		list3.value.push({ label: '日期', value: result.date });
		list3.value.push({ label: '时间', value: result.time });
		list3.value.push({ label: '开始时间', value: result.starttime });
		list3.value.push({ label: '结束时间', value: result.endtime });
		list3.value.push({ label: '定金', value: result.dprice });
		list3.value.push({ label: '订单服务费', value: result.orderprice });
		list3.value.push({ label: '订单金额', value: result.allprice });
		list3.value.push({ label: '应付尾款', value: result.payprice });

	}

	function statusText(status : string) {
		if (status === '1') {
			return "待支付";
		}
		if (status === '2') {
			return "待付定金";
		}
		if (status === '3') {
			return "待上门";
		}
		if (status === '4') {
			return "服务中";
		}
		if (status === '5') {
			return "待付尾款";
		}
		if (status === '6') {
			return "完成";
		}
		if (status === '7') {
			return "订单取消";
		}
		return "未知状态";
	}
	
	
	
	const orderPay = async() => {// 进行订单支付
		let param = {"wxid":userid.value,"id":orderid.value};
		const result = await wxPayOrderMM(param);
		console.log(JSON.stringify(result));
		uni.requestPayment({
		    provider: 'wxpay',    // 支付类型-固定值
		    timeStamp: result.timeStamp, // 时间戳（单位：秒）
		    nonceStr: result.nonceStr, // 随机字符串
		    package: result.packageValue, // 固定值
		    signType: result.signType, // 固定值
		    paySign: result.paySign, // 签名
		    success: (res) => {
		        console.log('success:' , JSON.stringify(res));
		        // console.log("支付成功");
				uni.redirectTo({
					url: '/pages/jz/payResult/payResult?flag=true'
				})
		    },
		    fail: (err) => {
		        console.log('fail:' , JSON.stringify(err));
		        // console.log("支付失败");
				uni.redirectTo({
					url: '/pages/jz/payResult/payResult?flag=false'
				})
		    }
		});
			
	}
	
	function backPage(e : any) {
		console.log(JSON.stringify(e));
		uni.redirectTo({
			url: '/pages/index/order'
		})
	}
</script>